import XEUtils from "xe-utils";

const getModelEvent = (renderOpts) => {
    return "input";
};

const getChangeEvent = (renderOpts) => {
    let type = "change";
    switch (renderOpts.name) {
        case "ElAutocomplete":
            type = "select";
            break;
        case "ElInput":
        case "ElInputNumber":
            type = "input";
            break;
    }
    return type;
};

const getNativeOns = (renderOpts, params) => {
    let nativeEvents = renderOpts.nativeEvents;
    let nativeOns = {};
    XEUtils.objectEach(nativeEvents, (func, key) => {
        nativeOns[key] = data => {
            let _len = arguments.length;
            let args = new Array(_len);
            for (let _key = 0; _key < _len; _key ++) {
                args[_key] = arguments[_key];
            }
            func.apply(void 0, [params, data].concat(args));
        };
    });
    return nativeOns;
};

const getEventOns = (renderOpts, params) => {
    let events = renderOpts.events;
    let ons = {};
    XEUtils.objectEach(events, (func, key) => {
        ons[key] = data => {
            let _len2 = arguments.length;
            let args = new Array(_len2);
            for (let _key2 = 0; _key2 < _len2; _key2 ++) {
                args[_key2] = arguments[_key2];
            }
            func.apply(void 0, [params, data].concat(args));
        };
    });
    return ons;
};

const getOns = (renderOpts, params, inputFunc, changeFunc) => {
    let events = renderOpts.events;
    let modelEvent = getModelEvent(renderOpts);
    let changeEvent = getChangeEvent(renderOpts);
    let isSameEvent = changeEvent === modelEvent;
    let ons = {};
    XEUtils.objectEach(events, (func, key) => {
        ons[key] = data => {
            let _len2 = arguments.length;
            let args = new Array(_len2);
            for (let _key2 = 0; _key2 < _len2; _key2 ++) {
                args[_key2] = arguments[_key2];
            }
            func.apply(void 0, [params, data].concat(args));
        };
    });
    if (inputFunc) {
        ons[modelEvent] = (targetEvent) => {
            inputFunc(targetEvent);
            if (events && events[modelEvent]) {
                events[modelEvent](params, targetEvent);
            }
            if (isSameEvent && changeFunc) {
                changeFunc(targetEvent);
            }
        };
    }
    if (!isSameEvent && changeFunc) {
        ons[changeEvent] = targetEvent => {
            let _len3 = arguments.length;
            let args = new Array(_len3);
            for (let _key3 = 0; _key3 < _len3; _key3 ++) {
                args[_key3] = arguments[_key3];
            }
            changeFunc.apply(void 0, args);
            if (events && events[changeEvent]) {
                events[changeEvent].apply(events, [params, targetEvent].concat(args));
            }
        };
    }
    return ons;
};

const getEditOns = (renderOpts, params) => {
    let $table = params.$table,
        row = params.row,
        column = params.column;
    return getOns(renderOpts, params, (value) => {
        // 处理 model 值双向绑定
        XEUtils.set(row, column.property, value);
    }, () => {
        // 处理 change 事件相关逻辑
        $table.updateStatus(params);
    });
}

export default {
    getNativeOns,
    getEditOns,
    getOns,
    getEventOns,
};
